ChemSpiPy: Getting Started

Before we start:

 First Steps

Start by importing ChemSpider:


In [1]:
from chemspipy import ChemSpider

Then connect to ChemSpider by creating a ChemSpider instance using your security token:


In [2]:
# Tip: Store your security token as an environment variable to reduce the chance of accidentally sharing it
import os
mytoken = os.environ['CHEMSPIDER_SECURITY_TOKEN']

cs = ChemSpider(security_token=mytoken)

All your interaction with the ChemSpider database should now happen through this ChemSpider object, cs.

Retrieve a Compound

Retrieving information about a specific Compound in the ChemSpider database is simple.

Let’s get the Compound with ChemSpider ID 2157:


In [3]:
comp = cs.get_compound(2157)
comp


Out[3]:

Now we have a Compound object called comp. We can get various identifiers and calculated properties from this object:


In [4]:
print(comp.molecular_formula)
print(comp.molecular_weight)
print(comp.smiles)
print(comp.common_name)


C_{9}H_{8}O_{4}
180.1574
CC(=O)Oc1ccccc1C(=O)O
Aspirin

Search for a name

What if you don’t know the ChemSpider ID of the Compound you want? Instead use the search method:


In [5]:
for result in cs.search('glucose'):
    print(result)


Compound(5589)
Compound(58238)
Compound(71358)
Compound(96749)
Compound(2006622)
Compound(5341883)
Compound(5360239)
Compound(9129332)
Compound(9281077)
Compound(9312824)
Compound(9484839)
Compound(9655623)

The search method accepts any identifer that ChemSpider can interpret, including names, registry numbers, SMILES and InChI.